<?php

namespace app\pc\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class Zi extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("UsersInvest");
        parent::__construct();

        $rows = Cache::get($this->Cstatic->get("sid"));
        $this->sid_user_id = $rows['id'];
        $this->sid_mobile = $rows['mobile'];
    }

    /**
     *  增加银行
     */
    //保存自然人信息
    public function save(){
        $result = $this->model->where('user_id',$this->sid_user_id)->find();

        if(isset($result['status'])){
            if($result['status']==0){
                return  failJson("你已经提交申请了");
            }
            if($result['status']==1){
                return  failJson("你已提交过申请,且申请通过");
            }

            if($result['status']==2){
                $update = 1;//审核失败重新进入审核流程
            }
        }

        $data = $this->RealData;
        $data['yidongdianhua'] = $this->sidRows['mobile'];
        $resultnumber = $this->model->where('id_number',$this->RealData['id_number'])->count();
        if($resultnumber>0){
            return   failJson("该身份证号码已存在");
        }
        if(empty($data)) {
            return   failJson("请输入合适的数据");
        }

        $data['user_id'] = $this->sid_user_id;

        if(isset($update)){
            $data['status'] = 0;
            $res = $this->model->save($data,['user_id'=>$this->sid_user_id]);
        }else{
            $res = $this->model->save($data);
        }

        if($res){
            return   sucessJson("提交成功请等待审核");
        }
        return  failJson("提交失败,请重新输入");
    }

    public function pinf(){

        $res = $this->model->where('user_id',$this->sid_user_id)->find();
        if(empty($res)){
            return   failJson("請完成认证");
        }


        $kkk=$this->daan;
        $d=["A"=>0,"B"=>1,"C"=>2,"D"=>3,"E"=>4,'F'=>5,"G"=>6,"H"=>7,"I"=>8];
        $model =   model("UsersTimu");

        $result = $model->select();
        $arr = [];
        foreach($result as $ks=>$vs){
            $vs = $vs->toArray();
            $arr[] = explode(",",$vs['getgrade']);
        }

        $total = 0;

        if(empty($arr)){
            return  failJson("题目不存在,请联系管理员");
        }
        $kkk = mbstringToArray($kkk,'utf-8');
        foreach($kkk as $k=>$v){
            $yeindex = $d[$v];
            if(isset($arr[$k][$yeindex])){
                $total +=$arr[$k][$yeindex];
            }

        }



        $grade = "R";
        //        grade
        if(bcsub($total,47)<=0){
            $grade = 'R1';
    }elseif($total>=48 && $total<=65){
            $grade = 'R2';
    }elseif($total>=66 && $total<=82){
            $grade = 'R3';
    }elseif($total>=83 && $total<=99){
            $grade = 'R4';
    }elseif($total>=100 && $total<=116){
            $grade = 'R5';
    }


        $data = ['total'=>$total,"daan"=>$this->daan,"grade"=>$grade];

        $resd  = $this->model->save($data,['user_id'=>$this->sid_user_id]);
        if($resd){
            return   sucessJson("提交成功请等待审核",['grade'=>$grade]);
        }
        return  failJson("提交失败,请重新输入");

    }


    //添加签名
    public function addimage(){

        $res = $this->model->where('user_id',$this->sid_user_id)->find();
        if(empty($res)){
            return   failJson("請完成认证");
        }

        if($res['status']==0){
            return   failJson("你的认证还在审核中..请等待");
        }
        /*   if($res['total']>0){
               return   failJson("你已完成評分");
           }*/

        $tdata =  saveBase64Image($this->image);

        if($tdata['code']==1){
            $stdata['image'] = $tdata['url'];

            $red = $this->model->save($stdata,['user_id'=>$this->sid_user_id]);
            if(!$red){
                return   failJson("签名失败");
            }
            return   sucessJson("签名成功");
        }
        return   failJson("签名失败");

    }

    //添加签名
    public function addimagehz(){

        $res = $this->model->where('user_id',$this->sid_user_id)->find();
        if(empty($res)){
            return   failJson("請完成认证");
        }

        if($res['status']==0){
            return   failJson("你的认证还在审核中..请等待");
        }
        /*   if($res['total']>0){
               return   failJson("你已完成評分");
           }*/

        $tdata =  saveBase64Image($this->image);

        if($tdata['code']==1){
            $stdata['htimage'] = $tdata['url'];

            $red = $this->model->save($stdata,['user_id'=>$this->sid_user_id]);
            if(!$red){
                return   failJson("签名失败");
            }
            addMessage($this->sid_user_id,"尊敬的用户:{$res['user_name']}您的实名认证已审核通过");
            sedMessageAll($this->sid_mobile,"ren","!");

            // "用户完成认证";
            return   sucessJson("签名成功");
        }
        return   failJson("签名失败");

    }




}
